<template>
	<view class="page pagebase">
		<view class="header">
			<img src="@/static/back.png" style="width: 48rpx;height: 48rpx;" class="back" @click="back" />
			<view class="header_title">
				关于我们
			</view>
		</view>
		<div class="hrader hraderbgc">
			<view class="hrader_top">
				<view class="hrader_top_left">
					<view class="shoudai">
						分期花费总额（元）
					</view>
				</view>
				<view class="hrader_top_right">
					<view class="result">
						{{dataList.daikuanhuafeizonge || '0'}}
					</view>
					<view class="jieshi">
						首付款总额+分期总额
					</view>
				</view>
			</view>
			<view class="hrader_buttom">
				<view class="hrader_buttom_items">
					<div class="hrader_buttom_items_title">
						首付总额
					</div>
					<div class="hrader_buttom_items_money">
						{{form.downPayment || '0'}}
					</div>
				</view>
				<u-icon name="plus" color="#fff"></u-icon>
				<view class="hrader_buttom_items">
					<div class="hrader_buttom_items_title">
						分期金额
					</div>
					<div class="hrader_buttom_items_money">
						{{dataList.fenqijine || '0'}}
					</div>
				</view>
				<u-icon name="plus" color="#fff"></u-icon>
				<view class="hrader_buttom_items">
					<div class="hrader_buttom_items_title">
						总利息
					</div>
					<div class="hrader_buttom_items_money">
						{{dataList.fenqizonglixi || '0'}}
					</div>
				</view>
				<u-icon name="plus" color="#fff"></u-icon>
				<view class="hrader_buttom_items">
					<div class="hrader_buttom_items_title">
						月供金额
					</div>
					<div class="hrader_buttom_items_money">
						{{dataList.yuegongjine || '0'}}
					</div>
				</view>
			</view>
		</div>
		<view class="modellist">
			<view class="items" @click="CarTypeConfirm">
				<view class="items_left">
					<text>车型</text>
				</view>
				<div style="display: flex;">
					<view class="items_rght_tips">
						{{CarTypeTips || '该项为选择项，请选择'}}
					</view>
					<view class="i" style="margin: 0 -13rpx 0 6rpx;">
						<u-icon name="arrow-right" color="#223263" size="20" style="padding: 0;"></u-icon>
					</view>
				</div>
			</view>

			<view class="items">
				<view class="items_left">
					<text>官方指导价</text>
				</view>
				<view class="items_rght">
					<view class="items_rght_tips">
						<u--input placeholder="自动计算生成" disabled border="surround" v-model="form.carPrice"
							inputAlign="right" disabledColor="#FAFBFF" placeholder-style="color: #223263"  color="#223263"></u--input>
					</view>
				</view>
			</view>
			<view class="items" @click="preductionShow = true">
				<view class="items_left">
					<text>产品</text>
				</view>
				<u-picker :show="preductionShow" :columns="preductionColumns" @confirm="preductionConfirm"
					@cancel="preductionCancel" keyName="label"></u-picker>
				<view class="items_rght_tips">
					{{preductionTips || '请手动选择产品，最终结果以终审为准'}}
				</view>
				<view class="i" style="margin: 0 -13rpx 0 6rpx;">
					<u-icon name="arrow-right" color="#223263" size="20" style="padding: 0;"></u-icon>
				</view>
			</view>
		</view>


		<view class="modellist">
			<view class="items" @click="yuegongfenqi">
				<view class="items_left">
					<text>月供期数</text>
				</view>
				<u-picker :show="qishuShow" :columns="biLiOrQiShu" @confirm="qishuConfirm" @cancel="qishuCancel"
					keyName="label"></u-picker>
				<view class="items_rght_tips">
					{{form.installmentNum || '该项为选择项，请选择'}}
				</view>
				<view class="i" style="margin: 0 -13rpx 0 6rpx;">
					<u-icon name="arrow-right" color="#223263" size="20" style="padding: 0;"></u-icon>
				</view>
			</view>
			<view class="items" style="position: relative;">
				<view class="items_left">
					<text>首付比例(%)</text>
				</view>
				<view class="items_rght">
					<view class="items_rght">
						<view class="items_rght_tips">
							<u--input placeholder="自动计算生成" border="surround" v-model="bili" inputAlign="right"
								disabledColor="#FAFBFF" disabled placeholder-style="color: #223263" color="#223263"></u--input>
						</view>
					</view>
				</view>
			</view>

			<view class="items">
				<view class="items_left">
					<text>首付金额</text>
				</view>
				<view class="items_rght">
					<view class="items_rght_tips">
						<u--input placeholder="请输入首付金额,单位(元)" @blur="downPaymentChange" border="surround"
							v-model="form.downPayment" inputAlign="right" disabledColor="#FAFBFF" placeholder-style="color: #223263" color="#223263"></u--input>
					</view>
				</view>
			</view>

			<view class="items">
				<view class="items_left">
					<text>费率(%)</text>
				</view>
				<view class="items_rght">
					<view class="items_rght_tips">
						{{annualizedRate || '自动计算生成'}}
					</view>
				</view>
			</view>
			<!-- 只有优享汇，畅享汇才展示 -->
			<view class="items" v-if="preductionTips != '尊享汇'">
				<view class="items_left">
					<text>车价开票金额</text>
				</view>
				<view class="items_rght">
					<view class="items_rght_tips">
						<u--input placeholder="请输入门店确定的开票金额" @blur="invoicingChange" border="surround"
							v-model="form.invoicingAmount" inputAlign="right" disabledColor="#FAFBFF" placeholder-style="color: #223263" color="#223263"></u--input>
					</view>
				</view>
			</view>
			<view v-if="isdisabled(preductionTips)" class="items" style="background-color: #FAFBFF;">
				<view class="items_left">
					<text>可以分期金额</text>
				</view>
				<view class="items_rght">
					<view class="items_rght_tips">
						<u--input placeholder="自动计算生成" @change="installmentAmountChange" border="surround"
							v-model="form.installmentAmount" inputAlign="right" disabledColor="#FAFBFF"
							:disabled="isdisabled(preductionTips)" placeholder-style="color: #223263" color="#223263"></u--input>
					</view>
				</view>
			</view>
			<view v-if="!isdisabled(preductionTips)" class="items">
				<view class="items_left">
					<text>可以分期金额</text>
				</view>
				<view class="items_rght">
					<view class="items_rght_tips">
						<u--input placeholder="请输入首付金额,单位(元)" @change="installmentAmountChange" border="surround"
							v-model="form.installmentAmount" inputAlign="right" disabledColor="#FAFBFF"
							:disabled="isdisabled(preductionTips)" placeholder-style="color: #223263" color="#223263"></u--input>
					</view>
				</view>
			</view>
		</view>


		<view class="modellist">
			<view class="items">
				<view class="items_left">
					<text>月供金额</text>
				</view>
				<view class="items_rght">
					<view class="items_rght_tips">
						{{dataList.yuegongjine || '该金额为近似值，具体以还款表为准'}}
					</view>
				</view>
			</view>
		</view>

		<view class="tips">
			*1.尊享汇产品部分区域费率有差异；2.尊享汇产品装备、盗
			抢、购置税都可分期；具体请您咨询平台客户经理
		</view>
		<u-button
			:customStyle="{border:'none',background: '#3E5CF6',width:'644rpx',color:'#fff',height:'90rpx',fontSize:'30rpx',borderRadius:'15rpx',marginTop:'30rpx'}"
			@click="submit">计算
		</u-button>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				shoufubili: 0,
				bili: null,
				annualizedRate: null,
				biLiOrQiShu: {},
				allData: [],
				preductionShow: false, //产品开关
				qishuShow: false, //期数开关
				chejia: 0, //计算车价
				CarTypeTips: '',
				form: {
					carPrice: null, //车价
					installmentNum: null, //月供期数
					productId: null, //产品id
					downPayment: null, //首付金额
					invoicingAmount: null, //车价开票金额
					installmentAmount: null, //分期金额
				},
				dataList: {},
				// 产品数据源
				preductionColumns: [

				],
				preductionTips: '',
				qishuTips: '',
			}
		},
		created() {
			this.getpreduction()
		},
		watch: {
			// 根据车价计算
			chejia: {
				// immediate:
				handler(newValue, oldValue) {
					this.form.downPayment = newValue * this.bili / 100
				}
			},
			// 根据首付比例计算
			// bili: {
			// 	handler(newValue, oldValue) {
			// 		this.form.downPayment = newValue / 100 * this.chejia
			// 	}
			// },
		},
		methods: {
			//计算
			async submit() {
				if (this.form.carPrice == null) {
					this.$ToastFail('请先选择车型')
					return
				}
				if (this.annualizedRate == null || this.form.productId == null) {
					this.$ToastFail('请先选择产品')
					return
				}
				if (this.form.installmentNum == null) {
					this.$ToastFail('请先选择期数')
					return
				}

				if (this.form.installmentAmount == null && this.preductionTips == '尊享汇') {
					this.$ToastFail('请输入分期金额')
					return
				}

				if (this.form.invoicingAmount == null && (this.preductionTips == '优享汇' || this.preductionTips ==
						'畅享汇')) {
					this.$ToastFail('请输入车价开票金额')
					return
				}
				const res = await this.$post(`/biz/wxCalculator/math`, {
					carPrice: this.form.carPrice, //车价
					installmentNum: this.form.installmentNum, //月供期数
					productId: this.form.productId, //产品id
					downPayment: this.form.downPayment, //首付金额
					annualizedRate: this.annualizedRate,
					invoicingAmount: this.form.invoicingAmount, //车价开票金额
					installmentAmount: this.form.installmentAmount, //分期金额
				})
				this.dataList = res.data.data
				this.form.installmentAmount = res.data.data.fenqijine
			},
			//分期金额改变事件
			installmentAmountChange() {
				this.$nextTick(function() {
					if (this.preductionTips == '尊享汇') {
						if (this.form.installmentAmount > 450000) {
							this.form.installmentAmount = 450000
						}
					}
					if (this.preductionTips == '优享汇' || this.preductionTips == '畅享汇') {
						if (this.form.installmentAmount > 300000) {
							this.form.installmentAmount = 300000
						}
					}
				})
			},
			//车辆开票价改变事件
			invoicingChange() {
				this.$nextTick(function() {

					if (this.form.installmentNum != '12' && this.form.installmentNum != null) {
						if (this.form.invoicingAmount >= 1 && this.form.invoicingAmount <= 20000) {
							this.$ToastFail('车价开票金额1-2万，仅能分期12期')
							this.form.installmentNum = 12
						}
					}
				})
			},
			//首付比例改变事件
			biliChange() {
				this.$nextTick(function() {
					if (this.bili < this.shoufubili) {
						this.$ToastFail('首付比例不可低于' + this.shoufubili + '%')
						this.bili = this.shoufubili
						this.form.downPayment = (this.bili * this.form.carPrice / 100).toFixed(2)
					}
				})
			},
			downPaymentChange() {
				this.$nextTick(function() {
					this.bili = (this.form.downPayment / this.form.carPrice * 100).toFixed(2)
					this.biliChange()
				})
			},
			// 分期金额是否禁用
			isdisabled(val) {
				var flg = false
				if (val == '优享汇' || val == '畅享汇') {
					flg = true
				}
				return flg
			},
			// 打开月供分期
			yuegongfenqi() {
				if (this.preductionTips == '') {
					this.$ToastFail('请先选择产品')
					return
				} else {
					this.qishuShow = true
				}
			},
			// 获取产品
			async getpreduction() {
				const res = await this.$get('/biz/wx/getProducts')
				console.log(res.data, 'res');
				if (res.statusCode == 200) {
					this.allData = res.data.data
					let data = res.data.data.map(items => {
						return {
							id: items.id,
							label: items.name
						}
					})
					this.preductionColumns = [data]
				}
			},
			// 先去选品牌，再选择车型
			CarTypeConfirm() {
				uni.navigateTo({
					url: '/pages/ke_hu_duan/wo_de_shen_qing/ping_pai'
				})
			},
			// 产品弹框确认按钮
			preductionConfirm(e) {
				this.form.productId = e.value[0].id
				this.preductionTips = e.value[0].label
				this.allData.forEach(items => {
					if (items.id == e.value[0].id) {
						this.annualizedRate = items.annualizedRate
						let qishuData = JSON.parse(items.qishuShoufubili).map((items, index) => {
							return {
								id: index + 1,
								label: items.num,
								bili: items.bili
							}
						})
						this.biLiOrQiShu = [qishuData]
					}
				})
				this.bili = this.biLiOrQiShu[0][0].bili
				this.shoufubili = this.biLiOrQiShu[0][0].bili
				this.form.installmentNum = this.biLiOrQiShu[0][0].label
				this.form.downPayment = this.form.carPrice * this.bili / 100
				this.installmentAmountChange()
				this.preductionShow = false
			},
			// 期数弹框确认按钮
			qishuConfirm(e) {
				if (1 < this.form.invoicingAmount && this.form.invoicingAmount < 20000 && e.value[0].label != 12 && this
					.preductionTips != '尊享汇') {
					this.$ToastFail('车价开票金额1-2万，仅能分期12期')
					this.invoicingChange()
					this.form.installmentNum = 12
					// this.form.downPayment = this.form.carPrice*this.bili/100
				} else {
					this.form.installmentNum = e.value[0].label
					this.bili = e.value[0].bili
					this.shoufubili = e.value[0].bili
					this.form.downPayment = this.form.carPrice * this.bili / 100
				}

				this.qishuShow = false
			},
			// 产品弹框取消按钮
			preductionCancel() {
				this.preductionShow = false
			},
			// 期数弹框取消按钮
			qishuCancel() {
				this.qishuShow = false
			},
			// 返回上一页
			back() {
				uni.navigateBack()
			}
		}
	}
</script>

<style>
	.page {
		width: 100%;
		height: 100vh;
		overflow-y: auto;
		padding: 0 32rpx 60rpx;
		box-sizing: border-box;
	}

	.hrader {
		padding: 44rpx;
		box-sizing: border-box;

		width: 686rpx;
		height: 324rpx;
		border-radius: 15rpx;
		margin: 10px auto 0;
	}

	.hrader_top {
		width: 100%;
		margin: 0 auto;
		/* border-bottom: 2rpx solid #2D91FF; */
		display: flex;
		justify-content: flex-end;
		align-items: flex-start;
		flex-direction: column;
		box-sizing: border-box;
	}

	.hrader_top_right {
		display: flex;
		align-items: center;
		/* font-size: 72rpx; */
		/* font-family: 'PingFang SC-Bold, PingFang SC'; */
		/* font-weight: bold; */
		/* color: #FFFFFF; */
		margin-top: 15rpx;
	}

	.result {
		font-size: 48rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 600;
		color: #FFFFFF;
		margin-right: 10rpx;
	}

	.jieshi {
		font-size: 24rpx;
		font-family: PingFang-SC, PingFang-SC;
		font-weight: 400;
		color: rgba(255, 255, 255, 0.68);
	}

	.shoudai {
		font-size: 28rpx;
		font-family: 'PingFang SC-Bold, PingFang SC';
		font-weight: 400;
		color: #FFFFFF;
	}

	.shoufu {
		font-size: 22rpx;
		font-family: 'PingFang SC-Bold, PingFang SC';
		font-weight: 400;
		color: #FFFFFF;
		margin-top: 4rpx;
	}

	.hrader_buttom {
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		height: 155rpx;
		width: 100%;
		margin: 0 auto;

	}

	.hrader_buttom_items_title {
		font-size: 24rpx;
		font-family: 'PingFang SC-Regular, PingFang SC';
		font-weight: 400;
		color: #FFFFFF;
	}

	.hrader_buttom_items_money {
		font-size: 30rpx;
		font-family: 'PingFang SC-Bold, PingFang SC';
		font-weight: bold;
		color: #FFFFFF;
		margin-top: 13rpx;
		text-align: center;
	}

	.modellist {
		width: 100%;
		background: #FAFBFF;
		border-radius: 15rpx;
		margin-top: 20rpx;
	}

	.items {
		overflow: hidden;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 31rpx;
		box-sizing: border-box;
		width: 690rpx;
		height: 97rpx;
		/* background-color: aqua; */
		margin: 0 auto 0;
	}

	.bi_tian {
		display: inline-block;
		color: red;
		margin: 10rpx 10rpx 0 0;
	}

	.items_left {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		font-size: 28rpx;
		font-size: 28rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 600;
		color: #223263;

	}

	.fenGe {
		width: 690rpx;
		height: 15rpx;
		background: #F4F4FA;
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		margin: 0 auto 0;
	}

	.tishi {
		height: 66rpx;
		font-size: 24rpx;
		font-family: 'PingFang SC-Regular, PingFang SC';
		font-weight: 400;
		color: #B1B1B1;
	}

	.xie_yi {
		display: flex;
		justify-content: flex-start;
		align-items: flex-start;
		height: 66rpx;
		font-size: 24rpx;
		font-family: 'PingFang SC-Regular, PingFang SC';
		font-weight: 400;
		color: #B1B1B1;
		margin: 34rpx 0 76rpx;
	}

	.items_rght_tips {
		font-size: 28rpx;
		font-family: 'PingFang SC-Regular, PingFang SC';
		font-weight: 400;
		color: #223263;
	}

	.u-input {
		/* background-color: red; */
		padding-right: 0rpx !important;
		margin-right: -2rpx;
	}

	.jingyong {
		background-color: #eee;
	}

	.shouQuanStyle {
		position: relative;
		width: 625rpx;
		height: 425rpx;
	}

	.titleBox {
		display: flex;
		justify-content: flex-end;
		align-items: center;
		margin-top: 33rpx;
		height: 57rpx;
	}

	.shouquantitle {
		font-size: 41rpx;
		font-family: 'PingFang SC-Medium, PingFang SC';
		font-weight: 500;
		color: #000000;
		margin-right: 142rpx;
	}

	.shouquancontent {
		padding: 0 60rpx;
		font-size: 24rpx;
		font-family: 'PingFang SC-Regular, PingFang SC';
		font-weight: 400;
		color: #4E4E4E;
		box-sizing: border-box;
		margin-top: 42rpx;
		margin-bottom: 87rpx;
	}

	.colse {
		font-size: 50rpx;
		font-family: 'PingFang SC-Regular, PingFang SC';
		font-weight: 400;
		color: #B2B2B2;
		margin-right: 50rpx;
	}

	.funStyle {
		width: 555rpx;
		margin: 0 auto;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.tips {
		width: 629rpx;
		margin: 34rpx auto 0;
		font-size: 24rpx;
		font-family: AppleSystemUIFont;
		color: #9098B1;
		line-height: 43rpx;

	}

	.header {
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 88rpx;
		margin-top: 88rpx;

	}

	.header_title {
		font-size: 32rpx;
		font-family: AppleSystemUIFont;
		color: #222625;
	}

	.back {
		position: absolute;
		top: 22rpx;
		left: 0;
	}
</style>